﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Zj.CourseManage.Mvc.Models;
using Microsoft.EntityFrameworkCore;
using Zj.CourseManage.Domain.Core;

namespace Zj.CourseManage.Mvc.Controllers
{
    public class HomeController : Controller
    {
        private readonly DbContext _ctx;
        public HomeController(DbContext ctx)
        {
            this._ctx = ctx;
        }
        //[IgnoreAntiforgeryToken]
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> About()
        {
            ViewData["Title"] = ViewData["Message"] = "学生信息统计";

            var list = await _ctx.Set<Student>().GroupBy(s => s.Birthday).Select(s => new StudentEnrollmentGroupVm() { StudentCount = s.Count(), EnrollmentDate = s.Key }).ToListAsync();
            return View(list);
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
